package com.banyiyi.simple.juc;

import lombok.Data;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;

/**
 * @ClassName AtomicDemo
 * @Description TODO
 * @Author yangwm
 * @Date 2023/3/22 16:31
 * @Version 1.0
 *
 * 3种基本原子类型：AtomicInteger AtomicLong AtomicBoolean
 *
 */
public class AtomicDemo {

    public static void main(String[] args) {

        // 创建原子类型
        AtomicInteger integer = new AtomicInteger();
        System.out.println(integer.get());
        // 取值并更新
        integer.getAndSet(3);
        System.out.println("交换之后的数值："+integer.get());
        // 取值并自增
        integer.getAndIncrement();
        System.out.println("自增之后的数值："+integer.get());
        // 取值并交换
        integer.compareAndSet(4,10);
        System.out.println("交换之后的数值："+integer.get());

        @Data
        class User{
            private String name;
            public volatile int age;
        }

        AtomicIntegerFieldUpdater<User> age = AtomicIntegerFieldUpdater.newUpdater(User.class, "age");
        User user = new User();
        age.set(user,20);
        age.incrementAndGet(user);
        System.out.println(age.get(user));


    }

}
